<!DOCTYPE html>
<html>
<head>
	<link rel="stylesheet" href="/css/bootstrap.min.css">
	<link rel="stylesheet" href="/css/font-awesome.min.css">
	<link rel="stylesheet" href="/css/ionicons.min.css">
	<link rel="stylesheet" href="/css/AdminLTE.min.css">
	<link rel="stylesheet" href="/plugins/iCheck/square/blue.css">
</head>
<body class="hold-transition skin-blue fixed sidebar-mini">
	<div class="wrapper">
		<div class="content-wrapper">
		  <div class="content-header">
		      <h1>
		                       基础数据
		        <small>城市信息维护</small>  
		      </h1>
	    	  <ol class="breadcrumb">
		        <li><a href="#"><i class="fa fa-dashboard"></i></a></li>
		        <li><a href="#">基础数据</a></li>
		        <li class="active">城市信息维护</li>
		      </ol>
		    </div>
			<div class="content">
				<div class="row">
					<div class="col-xs-12">
						<div class="box">
							<div class="box-body">
								<table id="cityList" class="table table-bordered table-hover" cellspacing="0" width="100%" style="height:300px">
								</table>
								</div>
							</div>
						</div>
					</div>
			</div>
		</div>
	</div>
	<script>
	$(document).ready(function(){
		loadData();
		initToolbar();
	});
	
	var addCity = function(){
        $('#cityForm')[0].reset();
        $('#cityDlg').modal('show');
    }
	
	var editCity1 = function(){
		$('#cityDlg').modal('show');
	}
	
	var delCity = function(id){
		swal({ 
		    title: "您确定要删除吗？", 
		    type: "warning", 
		    showCancelButton: true, 
		    closeOnConfirm: false, 
		    confirmButtonText: "确定", 
		    cancelButtonText:"取消",
		    confirmButtonColor: "#ec6c62" 
		}, function() { 
			$.post("<%=cx%>/interf/city/del",{"id" : id},function(data){
				var result = eval('(' + data + ')');
	            if(result.code == '0'){
	            	swal("成功", "删除成功", "success");
	                reload();
	            }
	            else{
	            	swal("错误", "删除失败", "error");
	            }
            });
		});
    }
	
	var editCity = function(id){
        $.post("<%=cx%>/interf/city/info" ,{"id" : id},function(data){
            var result = eval('(' + data + ')');
            if(result.code == '0'){
                  var dataJson = result.data;
                  $("#id").val(dataJson.id);
                  $("#name").val(dataJson.name);
                  $("#pingyin1").val(dataJson.pingyin1);
                  $("#pingyin2").val(dataJson.pingyin2);
                  $("#isHot").val(dataJson.isHot);
            }
            $("#cityDlg").modal('show');
        });
    }
	
	var saveCity = function(){
        var id = $('#id').val();
        var action = '';
        if(id == null){
            action = "<%=cx%>/interf/city/create";
        }
        else
        {
            action = "<%=cx%>/interf/city/update";
            
        }
        $("#cityForm").ajaxSubmit({
             url:action,
             success:function(data){
                 var result = eval('(' + data + ')');
                    if(result.code == '0'){
                       swal("成功","提交成功!","success"); 
                       reload();
                    }
                    else{
                    	swal("错误","提交失败,原因：" + result.msg,"error"); 
                    } 
             }
                
        });
     }
	
	var loadData = function(){
		$('#cityList').dataTable({
			searching: true,
            ordering: true,
            info: true,
            bServerSide: true,
            autoWidth: false,
            "dom": '<"toolbar">frtip',
            ajax: 
            {
                url: $("#contextpath").val() + '/interf/city/list',
                dataSrc: 'data',
                type:"POST",
            },
            pagingType: "simple_numbers",
            processing:true,
            serverSide: true,
	        columns: [                    
                {"data":"name","title":"城市名称"},
                {"data":"pingyin1","title":"拼音全拼"},
                {"data":"pingyin2","title":"拼音简称"},
                {"data":"isHot","title":"是否热门城市"},
                {"data":"alias","title":"别名"},
	            {"data":"","title":"操作"}
	        ],
	        columnDefs: [
	              {
	                  targets:5,
	                  render:function(a,b,c,d){
	                     var editBtn =  '<a type="button" class="btn btn-primary btn-xs btn-flat" onclick="editcity(\'' + c.id + '\');">编辑</a>';
	                     var delBtn = '<a type="button" class="btn btn-danger btn-xs btn-flat" onclick="delcity(\'' + c.id + '\');">删除</a>';
	                     var space = '&nbsp;&nbsp;';
	                     return editBtn + space + delBtn;
	                    
	                  }
	              },
	              {
	                  "defaultContent": "-",
	                  "targets": "_all"
	              }
	         ],
	         select: true,      
	    });
	}
	
	var initToolbar = function(){
        $("div.toolbar").html('<a type="button" class="btn btn-success btn-xs btn-flat" onclick="addCity()">新增</a>');
    }
	
    var reload = function(){
        $("#cityList").dataTable().fnDraw(false);
    }
    
    var closeDlg = function(){
        $("#cityDlg").modal('hide');
    }
	</script>
</body>
</html>
